package action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import pageModel.Good;
import pageModel.Json;
import service.GoodServiceI;

import com.opensymphony.xwork2.ModelDriven;
@Namespace("/")
@Action(value="goodAction", results = { @Result(name = "goodAdd", location = "/admin/spglAdd.jsp"), @Result(name = "goodEdit", location = "/admin/spglEdit.jsp")})
public class GoodAction extends BaseAction implements ModelDriven<Good>{
private GoodServiceI goodService;
Good good=new Good();
public GoodServiceI getGoodService() {
	return goodService;
}
@Autowired
public void setGoodService(GoodServiceI goodService) {
	this.goodService = goodService;
}
public Good getModel(){
	return good;
}
public String goodAdd() {
	return "goodAdd";
}

public String goodEdit() {
	return "goodEdit";
}

public void datagrid(){
	super.writeJson(goodService.datagrid(good));
}
public void add(){
	Json j=new Json();
	try{
		goodService.save(good);
		j.setSuccess(true);
		j.setMsg("添加成功!");
	}catch(Exception e){
		j.setMsg("添加失败,商品条形码编码重复!");
	}
	super.writeJson(j);
}
public void del(){
	Json j=new Json();
	try{
		goodService.remove(good.getIds());
	j.setSuccess(true);
	j.setMsg("删除成功!");
  }catch(Exception e){
	j.setMsg("删除失败!"); 
  }
	super.writeJson(j);
}
public void editor(){
	Json j=new Json();
	goodService.edit(good);
	j.setSuccess(true);
	j.setMsg("编辑成功!");
 
	super.writeJson(j);
}
}
